﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YodaMaster.Models;
using System.Data.Entity;
using System.Data;


namespace YodaMaster.Controllers
{
    public class CoursesController : Controller
    {

        public ProgDatabase Courses = new ProgDatabase();
        public FacultyRepository FacRepos = new FacultyRepository();

        //
        // GET: /Courses/

       
             public ViewResult Index(string sortOrder, string searchString)
        {
            Database.DefaultConnectionFactory = new System.Data.Entity.Infrastructure.SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");


            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            
            BrowseViewModel ViewModel = new BrowseViewModel();
            ViewModel.Faculty = FacRepos.GetFaculties();

            var faculties = from s in ViewModel.Faculty
                           select s;
            

            if (!String.IsNullOrEmpty(searchString))
            {
                faculties = faculties.Where(s => s.Text.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "Name desc":
                    faculties = faculties.OrderByDescending(s => s.Text);
                    break;
                default:
                    faculties = faculties.OrderBy(s => s.Text);
                    break;
            }


            ViewModel.Faculty = faculties.ToList();
            return View(ViewModel);
        }


        [HttpPost]
        public ActionResult Index(BrowseViewModel postmodel)
        {
            postmodel.SelectedFaculty = Request.Form["SelectedFaculty"];
            return RedirectToAction("Index2", postmodel);
        }


        public ActionResult Index2(BrowseViewModel postmodel, string selected, string sortOrder, string searchString, string search_id)
        {
            if(String.IsNullOrEmpty(selected)) selected = postmodel.SelectedFaculty;
            BrowsePostViewModel PostViewModel = new BrowsePostViewModel();
            PostViewModel.courses = new List<string>();
            PostViewModel.courses_ids = new List<int>();
            PostViewModel.courses_names = new List<string>();
            PostViewModel.SelectedFaculty = postmodel.SelectedFaculty;


            var related = from val in Courses.Courses where val.Value == selected select val.Key;
            foreach (var course in related)
            {

                PostViewModel.courses.Add(course.name + " : " + course.number.ToString());
                PostViewModel.courses_names.Add(course.name);
                PostViewModel.courses_ids.Add(course.number);

            }

            var vitem = (from item in PostViewModel.courses
                         select new SelectListItem
                         {
                             Text = item,
                             Value = item
                         });
            PostViewModel.Course = vitem.ToList();
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name desc" : "";
            ViewBag.IDSortParm = sortOrder == "ID" ? "ID desc" : "ID";
            var courses = from s in PostViewModel.courses
                           select s;
            if (!String.IsNullOrEmpty(searchString))
            {
                courses = courses.Where(s => s.Split(':')[0].ToUpper().Contains(searchString.ToUpper()));
            }
            if (!String.IsNullOrEmpty(search_id))
            {
                courses = courses.Where(s => s.Split(':')[1].ToUpper().Contains(search_id.ToUpper()));
            }
            switch (sortOrder)
            {
                case "Name desc":
                    courses = courses.OrderByDescending(s => s.Split(':').ElementAt(0));
                    break;
                case "ID desc":
                    courses = courses.OrderByDescending(s => s.Split(':').ElementAt(1));
                    break;
                case "ID":
                    courses = courses.OrderBy(s => s.Split(':').ElementAt(1));
                    break;
                default:
                    courses = courses.OrderBy(s => s.Split(':').ElementAt(0));
                    break;
            }
             if (!String.IsNullOrEmpty(selected)) PostViewModel.SelectedFaculty = selected;
            PostViewModel.courses = courses.ToList();
            return View(PostViewModel);
        }
        [HttpPost]
        public ActionResult Index2(BrowsePostViewModel postmodel)
        {
            postmodel.SelectedCourse = Request.Form["SelectedCourse"];
            return RedirectToAction("Details", postmodel);
        }


        //
        // GET: /Store/Details
        public ActionResult Details(BrowsePostViewModel postmodel, string selected_c, string selected_f)
        {
            if(!String.IsNullOrEmpty(selected_c)) postmodel.SelectedCourse = selected_c;
            if(!String.IsNullOrEmpty(selected_f)) postmodel.SelectedFaculty = selected_f;
            if (!String.IsNullOrEmpty(postmodel.SelectedCourse)) selected_c = postmodel.SelectedCourse;
            if (!String.IsNullOrEmpty(postmodel.SelectedFaculty)) selected_f = postmodel.SelectedFaculty;
            var related = from val in Courses.Courses where val.Value == postmodel.SelectedFaculty select val.Key;
            Table Table;
            foreach (Course course in related)
            {
                string Sel = course.name + " : " + course.number.ToString();
                if (Sel == postmodel.SelectedCourse)
                {
                    Table = new Table(course);
                    return View(Table);
                }

               

            }
            return View();
        }
    }
}
